
#ifndef _____________WARD___FORCE_H
#define _____________WARD___FORCE_H

#include "driver.h"
#include "Troop.h"

static string fitoa(int num, int maxlen=50)
{
	char* buf = new char[maxlen];
		_itoa_s(num,buf,maxlen,10);
	string ret(buf);
	delete buf;
	return ret;
}


class Force
{
public:
	

	Force(int o = 0) { owner = o;}

	Force(int o, int n, int r) {owner = o; add(n,r);}
	//add n troops to this force, with an average readiness of rdy (in range [1,5])
	void add(int n, int rdy = 2);

	//add a force to this one..
	friend void merge(Force &source, Force &dest, int n);

	//remove some troops from this force and return it
	Force split(int n);

	int getSize() { return (int)men.size(); }
	int getRdy() { if(men.size() <= 0) return 0;  unsigned int n=0; unsigned int i; for(i = 0; i < men.size(); i++) n+= men[i].getReadiness(); return n/men.size();}
	string getRdyStr() { return readydescriptors[getRdy()-1]; }	
	//removes n troops
	void destroy(int n);
	void train(int  n);
	//returns a (sorted) vector of attack values
	vector <int> attack();

	//returns a (sorted) vector of attack values
	vector <int> attack(int n, int mod=0);

	int getOwner() { return owner; }
	void setOwner(int o) { owner = o; }

private:
	vector <Troop> men;
	int owner;
};


static void merge(Force &source, Force &dest, int n)
{
int i;
int size = min(n,dest.getSize());
for(i = 0; i < size; i++)
{
	source.men.push_back(*(dest.men.rbegin()));
	dest.men.pop_back();
}
}





//Attack
//Executes an attack and returns the winner ownerID
//
static int attack(Force &attacker, Force &defender, bool aShip, bool dShip)
{
	int atkNumDead = 0;
	int defNumDead = 0;
	int size;
	int i,j;

	int tad = 0;
	int tdd = 0;

	while((attacker.getSize())&&(defender.getSize()))
	{
	tad +=atkNumDead;
	tdd +=defNumDead;
	atkNumDead = 0;
	defNumDead = 0;
	vector <int> atk = attacker.attack(min(attacker.getSize(),3));
	vector <int> def;
	if((!aShip)&&(dShip))
		def = defender.attack(min(attacker.getSize(),2),1);
	else
		def = defender.attack(min(attacker.getSize(),2));

	size = (int)min(atk.size(), def.size());

	
	for(i = size-1; i > 0; i--)		
	{
		for(j = size-1; j > 0; j--)
			if(def[j] > 0) //if there are still defenders on this level..
			{
				if(j > i)	//if the defenders are higher level than the attackers..
				{
					int num = min(atk[i], def[j]);
					atk[i] -= num; 
					atkNumDead += num;
					def[j] -= num;		//used up in battle, but not killed
				}
				if(j == i)	//if the defenders are higher level than the attackers..
				{
					int num = min(atk[i], def[j]);
					atk[i] -= num; 
					def[j] -= num;		//used up in battle, but not killed

					if((aShip)&&(!dShip))		//attacker ship is present, not defensive ship
					{
						if((rand()%100) < 5)
							defNumDead += num;
						else
							atkNumDead += num;
					}
					else
							atkNumDead += num;
				}
				else if(j < i)		//j < i
				{
					int num = min(atk[i], def[j]);
					atk[i] -= num;	//used up in battle..
					defNumDead += num;
					def[j] -= num;	//actually killed..
				}
			}
			if(atkNumDead >= attacker.getSize())
				break;
			if(defNumDead >= defender.getSize())
				break;
	}

	attacker.destroy(min(attacker.getSize(), atkNumDead));
	defender.destroy(min(defender.getSize(), defNumDead));
	}

	myMSG.add("Player " + 	fitoa(attacker.getOwner()+1,3) + " lost " + fitoa(tad,10) + ".");
if(defender.getOwner() != -1)
	myMSG.add("Player " + 	fitoa(defender.getOwner()+1,3) + " lost " + fitoa(tdd,10) + ".");

	if(attacker.getSize() == 0) return defender.getOwner();
	else	return attacker.getOwner();

}


#endif